package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


// 攻略
@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    // 旅游攻略模块下的攻略明细
    @GetMapping("/detail")// 明细
    public Object detail(String id){
        // 旅游攻略下的查询攻略
        Strategy strategy = strategyService.get(id);

        // 攻略阅读数+1
        strategyStatisVORedisService.viewnumIncrease(id, 1);

        return JsonResult.success(strategy);
    }

    // 旅游攻略按钮之分页
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }

    // 主题查询(旅游攻略全部按钮)
    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.listAll());
    }

    // 攻略评论按钮(必须先登录才能评论)
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){

        // 设置用户信息(拷贝属性)参数1:源对象 复制到参数2:目标对象
        BeanUtils.copyProperties(userInfo,comment);
        // 用户id无法拷贝,属性不一样
        comment.setUserId(userInfo.getId());
        // 保存
        strategyCommentService.save(comment);
        // 攻略评论数+1
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(),1);

        return JsonResult.success();
    }

    // 评论分页
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    // 攻略评论点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    // 统计数据:评论/点赞/收藏回显等等(vo对象管理)
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }

    // 攻略收藏统计+1
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){

        // true 收藏操作 , false 取消收藏操作
       boolean ret = strategyStatisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
    // strategyThumbup 顶(点赞操作)
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){

        // true 顶成功 , false 今天顶过了,不允许再顶
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
}
